package packets;

import java.io.UnsupportedEncodingException;

import world.WorldState;

import exceptions.UnrecoverableException;
import network.MCConnection;


public abstract class PacketHandler {
	

	protected WorldState worldstate;
	
	public PacketHandler(WorldState worldstate) {
		this.worldstate = worldstate;
	}
	
	public abstract void handle(MCConnection conn) throws UnrecoverableException;
	
	/**
	 * Converts a Java-string to a sequence of bytes
	 * in big-endian encoding. The output can be used
	 * to be send back over the network directly. 
	 */
	public static byte[] StringToBytes(String msg) {
		try {
			return msg.getBytes("UTF-16BE");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return new byte[0];
	}
	
	
	public WorldState getWorldState() {
		return worldstate;
	}
	
	
	
}

//public class Packet {
//	
//	private int command;
//	private byte[] message;
//	
//	public Packet(int commandcode, byte[] message) {
//		this.command = commandcode;
//		this.message = message;
//	}
//	
//	public byte[] toByte() {
//		byte[] header = new byte[3];
//		header[0] = (byte) command;
//		
//		//this is what i call: expensive :(
//		short length = (short) (message.length / 2);
//		header[1] = (byte)((length >> 8) & 0xff);
//		header[2] = (byte)(length & 0xff);
//
//		return concat(header, message);
//	}
//	
//	public static byte[] concat(byte[] first, byte[] second) {
//		  byte[] result = Arrays.copyOf(first, first.length + second.length);
//		  System.arraycopy(second, 0, result, first.length, second.length);
//		  return result;
//		}
//
//}
